/*
 * QuestionnaireOpt.java
 */

package dialog;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 * Dialog box that gets the parameters for the questionnaire. Only one
 * repetition. A list of possible questionnaires is supposed to show up.
 *
 * @author Justin
 */
public class QuestionnaireOpt extends javax.swing.JDialog {
	/** The CSV file for the Questionnaire. */
	String file = "";

    /** Creates new form QuestionnaireOpt */
    public QuestionnaireOpt (java.awt.Frame parent, boolean modal) {
        super(parent, modal);
		initComponents();
		file = getFileFromFileChooser();
		updateFileLabel();
    }

	/**
	 * Updates the label showing what file we've currently selected.
	 */
	private void updateFileLabel() {
		if (file != "" && file != null) {
			fileLabel.setText (file);
		}
	}

	/**
	 * Opens a file chooser dialog, and returns the name of the file to open.
	 */
	private String getFileFromFileChooser() {
		JFileChooser fileChooser = new JFileChooser("./questionnaire/");
        fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        fileChooser.addChoosableFileFilter (new CSVFilter());

        fileChooser.showOpenDialog(this);
        if(fileChooser.getSelectedFile() != null)
            return fileChooser.getSelectedFile().getName();
		else
			return null;
	}

	/**
	 * Filter for CSV files for file chooser.
	 */
	class CSVFilter extends javax.swing.filechooser.FileFilter {
		public boolean accept(File file) {
			String filename = file.getName();
			return filename.endsWith(".csv");
		}

		public String getDescription() {
			return "Comma Separated Values (*.csv)";
		}
	}

	/**
	 * Returns parameter list for config screen.
	 * 
	 * @return parameters.
	 */
	public String getParamsList()
    {
		String correctPay = correctPayField.getText();
        return "1," + file + "," + correctPay;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        submitButton = new javax.swing.JButton();
        fileLabel = new javax.swing.JLabel();
        chooseFileButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        correctPayField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Parameters for Questionnaire");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("No. of Game Repetitions: 1");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Questionnaire file:");

        submitButton.setText("OK");
        submitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitButtonActionPerformed(evt);
            }
        });

        fileLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        fileLabel.setText("No file chosen yet.");

        chooseFileButton.setFont(new java.awt.Font("Tahoma", 0, 12));
        chooseFileButton.setText("Choose a different file.");
        chooseFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseFileButtonActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Pay for correct answers ($):");

        correctPayField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        correctPayField.setText("0.10");
        correctPayField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                correctPayFieldActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(176, 176, 176)
                        .add(submitButton))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                            .add(jLabel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                            .add(layout.createSequentialGroup()
                                .add(67, 67, 67)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(chooseFileButton)
                                    .add(fileLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)))
                            .add(layout.createSequentialGroup()
                                .add(29, 29, 29)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createSequentialGroup()
                                        .add(jLabel3)
                                        .add(29, 29, 29)
                                        .add(correctPayField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 64, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .add(jLabel2))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 114, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .add(jLabel1)
                .add(28, 28, 28)
                .add(jLabel4)
                .add(18, 18, 18)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fileLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(chooseFileButton)
                .add(34, 34, 34)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(correctPayField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(36, 36, 36)
                .add(submitButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	/**
	 * If the dialog is submitted, then check if we have a file. If we do, then
	 * exit this dialog.
	 *
	 * @param evt
	 */
	private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
		if (file != null && file != "")
			this.setVisible(false);
		else {
			JOptionPane.showMessageDialog(this, "Choose a file first.",
				"No file selected.",
				JOptionPane.ERROR_MESSAGE);
		}
	}//GEN-LAST:event_submitButtonActionPerformed

	private void chooseFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseFileButtonActionPerformed
		file = getFileFromFileChooser();
		updateFileLabel();
	}//GEN-LAST:event_chooseFileButtonActionPerformed

	private void correctPayFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_correctPayFieldActionPerformed
		// TODO add your handling code here:
	}//GEN-LAST:event_correctPayFieldActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                QuestionnaireOpt dialog =
                        new QuestionnaireOpt(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton chooseFileButton;
    private javax.swing.JTextField correctPayField;
    private javax.swing.JLabel fileLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton submitButton;
    // End of variables declaration//GEN-END:variables

}
